package com.qiniu.shortvideo.record;

import android.content.Context;
import android.opengl.GLSurfaceView;

import com.qiniu.pili.droid.shortvideo.PLAudioEncodeSetting;
import com.qiniu.pili.droid.shortvideo.PLCameraSetting;
import com.qiniu.pili.droid.shortvideo.PLFaceBeautySetting;
import com.qiniu.pili.droid.shortvideo.PLMicrophoneSetting;
import com.qiniu.pili.droid.shortvideo.PLRecordSetting;
import com.qiniu.pili.droid.shortvideo.PLShortVideoRecorder;
import com.qiniu.pili.droid.shortvideo.PLVideoEncodeSetting;
import com.qiniu.shortvideo.utils.PLConstant;
import com.qiniu.shortvideo.utils.RecordSettings;

/**
 * 短视频采集管理类
 * Created by Su on 2017/11/9.
 */
public class PLRecordMgr {
    private volatile static PLRecordMgr instance = null;

    public static PLRecordMgr getInstance() {
        if (instance == null) {
            synchronized (PLRecordMgr.class) {
                if (instance == null)
                    instance = new PLRecordMgr();
            }
        }
        return instance;
    }


    // ========================= 录制对象创建 ==================================
    private PLShortVideoRecorder mShortVideoRecorder;  // 录制对象

    /**
     * 创建录制对象
     */
    public PLShortVideoRecorder getShortVideoRecorder(Context context, GLSurfaceView preview) {
        if (mShortVideoRecorder == null) {
            mShortVideoRecorder = new PLShortVideoRecorder();
        }
        // 将拍摄所选择的参数配置到SDK中
        mShortVideoRecorder.prepare(preview, getCameraSetting(), getMicrophoneSetting(), getVideoEncodeSetting(context),
                getAudioEncodeSetting(), getFaceBeautySetting(), getRecordSetting());
        mShortVideoRecorder.setRecordSpeed(getRecordSpeed());
        return mShortVideoRecorder;
    }

    // ========================== 摄像头参数采集 ===================================
    private PLCameraSetting mCameraSetting;             // 采集参数

    /**
     * 配置摄像头采集参数
     */
    public PLCameraSetting getCameraSetting() {
        if (mCameraSetting == null) {
            mCameraSetting = new PLCameraSetting();
            mCameraSetting.setCameraId(chooseCameraFacingId());
            mCameraSetting.setCameraPreviewSizeRatio(getPreviewSizeRatio());
            mCameraSetting.setCameraPreviewSizeLevel(getPreviewSizeLevel());
        }
        return mCameraSetting;
    }

    /**
     * 0表示 4：3，  1表示 16：9
     */
    private PLCameraSetting.CAMERA_PREVIEW_SIZE_RATIO getPreviewSizeRatio() {
        return RecordSettings.PREVIEW_SIZE_RATIO_ARRAY[1];
    }

    /**
     * 2表示480P
     */
    private PLCameraSetting.CAMERA_PREVIEW_SIZE_LEVEL getPreviewSizeLevel() {
        return RecordSettings.PREVIEW_SIZE_LEVEL_ARRAY[4];
    }

    private PLCameraSetting.CAMERA_FACING_ID chooseCameraFacingId() {
        if (PLCameraSetting.hasCameraFacing(PLCameraSetting.CAMERA_FACING_ID.CAMERA_FACING_3RD)) {
            return PLCameraSetting.CAMERA_FACING_ID.CAMERA_FACING_3RD;
        } else if (PLCameraSetting.hasCameraFacing(PLCameraSetting.CAMERA_FACING_ID.CAMERA_FACING_FRONT)) {
            return PLCameraSetting.CAMERA_FACING_ID.CAMERA_FACING_FRONT;
        } else {
            return PLCameraSetting.CAMERA_FACING_ID.CAMERA_FACING_BACK;
        }
    }

    // ======================== 麦克风采集相关 =========================
    private PLMicrophoneSetting microphoneSetting;      //  麦克风采集选项

    /**
     * 麦克风采集参数
     */
    private PLMicrophoneSetting getMicrophoneSetting() {
        if (microphoneSetting == null)
            microphoneSetting = new PLMicrophoneSetting();
        return microphoneSetting;
    }

    // =============================== 视频编码 ================================
    private PLVideoEncodeSetting videoEncodeSetting;    //  视频编码选项

    /**
     * 视频编码
     */
    private PLVideoEncodeSetting getVideoEncodeSetting(Context context) {
        if (videoEncodeSetting == null) {
            videoEncodeSetting = new PLVideoEncodeSetting(context);
            videoEncodeSetting.setEncodingSizeLevel(getEncodingSizeLevel());
            videoEncodeSetting.setEncodingBitrate(getEncodingBitrateLevel());
        }
        return videoEncodeSetting;
    }

    /**
     * 7表示 480 * 480
     */
    private PLVideoEncodeSetting.VIDEO_ENCODING_SIZE_LEVEL getEncodingSizeLevel() {
        return RecordSettings.ENCODING_SIZE_LEVEL_ARRAY[14];
    }

    /**
     * 编码率： 1表示 800 Kbps
     */
    private int getEncodingBitrateLevel() {
        return RecordSettings.ENCODING_BITRATE_LEVEL_ARRAY[1];
    }

    // =========================== 音频编码 ==================================
    private PLAudioEncodeSetting audioEncodeSetting;    //  音频编码选项

    /**
     * 音频编码
     */
    private PLAudioEncodeSetting getAudioEncodeSetting() {
        if (audioEncodeSetting == null)
            audioEncodeSetting = new PLAudioEncodeSetting();
        return audioEncodeSetting;
    }

    // ======================= 美颜选项 ======================================
    private PLFaceBeautySetting faceBeautySetting;      //  美颜选项

    /**
     * 美颜选项
     */
    private PLFaceBeautySetting getFaceBeautySetting() {
        if (faceBeautySetting == null)
            faceBeautySetting = new PLFaceBeautySetting(1.0f, 0.5f, 0.5f);
        return faceBeautySetting;
    }

    // ======================== 录制选项 ==============================
    private PLRecordSetting recordSetting;              //  录制选项

    /**
     * 获取最大录制时长
     */
    public long getMaxRecordDuration() {
        return getRecordSetting().getMaxRecordDuration();
    }

    /**
     * 录制选项
     */
    private PLRecordSetting getRecordSetting() {
        if (recordSetting == null) {
            recordSetting = new PLRecordSetting();
            recordSetting.setMaxRecordDuration((long) (RecordSettings.DEFAULT_MAX_RECORD_DURATION * getRecordSpeed()));
            recordSetting.setVideoCacheDir(PLConstant.VIDEO_STORAGE_DIR);
            recordSetting.setVideoFilepath(PLConstant.RECORD_FILE_PATH);
        }
        return recordSetting;
    }

    /**
     * 录制速度： 3表示 1x 速率
     */
    public double getRecordSpeed() {
        return RecordSettings.RECORD_SPEED_ARRAY[3];
    }
}
